import { useUserInfoStore } from '@/stores/modules/userInfo'
import ZcNprogress from 'nprogress'
import { Router } from 'vue-router'

export const setPermissions = async (router: Router) => {
  // 设置进度条的配置项
  ZcNprogress.configure({
    easing: 'ease',
    speed: 200,
    trickleSpeed: 200,
    showSpinner: false
  })

  router.beforeEach((to, from, next) => {
    ZcNprogress.start()
    const { token, getUserInfo } = useUserInfoStore()
    const hasToken = token
    if (!hasToken) {
      if (to.path === '/login') {
        next()
      } else {
        next('/login')
      }
    } else {
      getUserInfo()
      if (to.path === '/login') {
        next('/')
      } else {
        next()
      }
    }
  })

  router.afterEach(() => {
    if (ZcNprogress.status) ZcNprogress.done()
    console.log('路由加载完成！')
  })
}
